import apiClient from "../apiClient";

export interface CategoryItem {
	id: number;
	name: string;
	weigh: number;
	status: 0 | 1;
	createtime: string;
	updatetime: string;
}

export interface ApiResponse<T> {
	total: number;
	items: T[];
}

export enum FieldType {
	TEXT = "text",
	NUMBER = "number",
	SELECT = "select",
	IMAGE = "image",
	FILE = "file",
	TEXTAREA = "textarea",
	RADIO = "radio",
	CHECKBOX = "checkbox",
	RICHTEXT = "richtext",
}

export interface TaskField {
	id: number;
	task_id: number;
	field_id: number;
	field_type: FieldType;
	field_label: string;
	required: string;
	placeholder: string;
	sort: number | null;
	createtime: number;
	updatetime: number;
	deletetime: number | null;
	required_text: string;
	options?: { label: string; value: string | number }[];
}

export interface TaskItem {
	id: number;
	title: string;
	description: string;
	creator_id: number | null;
	price: string;
	auditor_id: number;
	status: number;
	createtime: number;
	deletetime: number | null;
	category_id: number;
	updatetime: number;
	attachment: string;
	category: {
		id: number;
		pid: number;
		type: string;
		name: string;
		nickname: string;
		flag: string;
		image: string;
		keywords: string;
		description: string;
		diyname: string;
		createtime: number;
		updatetime: number;
		weigh: number;
		status: number;
	};
	status_text: string;
	taskFields: {
		fieldId: number;
		fieldType: string;
		fieldLabel: string;
		sort: number;
		required: boolean;
		placeholder: string;
	}[];
}

export interface TaskAddData {
	title: string;
	category_id: number;
	price: string | number;
	reviewer: string;
	status: number;
	taskFields: TaskField[];
}

export enum TaskApi {
	CategoryList = "/task_category/index",
	CategoryAdd = "/task/task_category/add",
	CategoryEdit = "/task/task_category/edit",
	CategoryDelete = "/task/task_category/del",
	CategoryInfo = "/task/task_category/info",
	TaskAdd = "/task/task/add",
	TaskList = "/task/index",
	TaskInfo = "/task/info",
	TaskFieldList = "/task/field/index",
	TaskFieldAdd = "/task/field/add",
	TaskFieldEdit = "/task/field/edit",
	TaskFieldDelete = "/task/field/del",
	TaskFieldInfo = "/task/field/info",
	TaskSubmit = "/task/submit",
}

// 任务分类相关接口
const categoryList = (params: { page: number; limit: number }) =>
	apiClient.get<ApiResponse<CategoryItem>>({
		url: TaskApi.CategoryList,
		params,
	});

const categoryAdd = (
	data: Omit<CategoryItem, "id" | "createtime" | "updatetime">,
) =>
	apiClient.post({
		url: TaskApi.CategoryAdd,
		data,
	});

const categoryEdit = (data: Partial<CategoryItem> & { id: number }) =>
	apiClient.post({
		url: TaskApi.CategoryEdit,
		data,
	});

const categoryDelete = (id: number) =>
	apiClient.post({
		url: TaskApi.CategoryDelete,
		data: { id },
	});

const categoryInfo = (id: number) =>
	apiClient.get<CategoryItem>({
		url: TaskApi.CategoryInfo,
		params: { id },
	});

const taskAdd = (data: TaskAddData) =>
	apiClient.post({
		url: TaskApi.TaskAdd,
		data,
	});

const taskList = (params: { page: number; limit: number }) =>
	apiClient.get<ApiResponse<TaskItem>>({
		url: TaskApi.TaskList,
		params,
	});

// 获取任务详情
const taskInfo = (id: number) =>
	apiClient.get<TaskItem>({
		url: TaskApi.TaskInfo,
		params: { id },
	});

const taskFieldList = (params: { page: number; limit: number }) =>
	apiClient.get<ApiResponse<TaskField>>({
		url: TaskApi.TaskFieldList,
		params,
	});

const taskFieldAdd = (
	data: Omit<TaskField, "id" | "createtime" | "updatetime">,
) =>
	apiClient.post({
		url: TaskApi.TaskFieldAdd,
		data,
	});

const taskFieldEdit = (data: Partial<TaskField> & { id: number }) =>
	apiClient.post({
		url: TaskApi.TaskFieldEdit,
		data,
	});

const taskFieldDelete = (id: number) =>
	apiClient.post({
		url: TaskApi.TaskFieldDelete,
		data: { id },
	});

const taskFieldInfo = (id: number) =>
	apiClient.get<TaskField>({
		url: TaskApi.TaskFieldInfo,
		params: { id },
	});

interface TaskSubmitField {
	field_id: number;
	value: string;
}

interface TaskSubmitData {
	task_id: number;
	fields: Record<string, TaskSubmitField>;
}

// 提交任务
const taskSubmit = (data: TaskSubmitData) =>
	apiClient.post({
		url: TaskApi.TaskSubmit,
		data,
	});

export default {
	categoryList,
	categoryAdd,
	categoryEdit,
	categoryDelete,
	categoryInfo,
	taskAdd,
	taskList,
	taskInfo,
	taskFieldList,
	taskFieldAdd,
	taskFieldEdit,
	taskFieldDelete,
	taskFieldInfo,
	taskSubmit,
};
